package com.zys.el.user.util.Analyzer;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import com.huaban.analysis.jieba.WordDictionary;
import org.springframework.stereotype.Component;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Component
public class AnalyzerUtil {
    public Set<String> ik(String text) throws IOException {
        StringReader t = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(t, true);
        Set<String> ikWord = new HashSet<>();
        Lexeme lex;
        while ((lex = ik.next()) != null) {
            ikWord.add(lex.getLexemeText());
        }

        Iterator<String> iterator = ikWord.iterator();
        while (iterator.hasNext()) {
            String s = iterator.next();
            if (s == "~" ||
                    s == "`" || s == "!" ||
                    s == "#" || s == "@" ||
                    s == "$" || s == "%" ||
                    s == "&" || s == "^" ||
                    s == "*" || s == "(" ||
                    s == ")" || s == "（" ||
                    s == "）" || s == "-" ||
                    s == "_" || s == "+" ||
                    s == "=" || s == "]" ||
                    s == "[" || s == "}" ||
                    s == "{" || s == "\\" ||
                    s == "|" || s == "\"" ||
                    s == "?" || s == ">" ||
                    s == "." || s == "<" ||
                    s == "," || s == "，" ||
                    s == "、" || s == "？" ||
                    s == "……" || s == "！" ||
                    s == "￥" || s == "。" ||
                    s == ";" || s == "；" ||
                    s == "“" || s == "‘" ||
                    s == "：" || s == "'" ||
                    s == "《" || s == "”" ||
                    s == "》"
            ) {
                iterator.remove();
            } else if (s.length() > 2 && s.charAt(0) > 130) {
                iterator.remove();
            } else if (s.length() < 2 || s.length() > 8) {
                iterator.remove();
            }
        }


        return ikWord;
    }

    public Set<String> jieba(String text) throws IOException {
        WordDictionary wordDictionary = WordDictionary.getInstance(); // 获取词库

        JiebaSegmenter jiebaSegmenter = new JiebaSegmenter();
        List<SegToken> segTokens = jiebaSegmenter.process(text, JiebaSegmenter.SegMode.SEARCH); // 分词处理
        Iterator<SegToken> iterator = segTokens.iterator(); //迭代器

        SegToken segToken;
        Set<String> jiebaWord = new HashSet<>(); // 返回数据
        while (iterator.hasNext()) {
            segToken = iterator.next();
            jiebaWord.add(segToken.word);
        }
        return jiebaWord;
    }

}
